/*global Adebug*/
const powerTree = [{
  code: 'app',
  title: '平台',
  child: [{
    code: 'app',
    title: '应用列表',
    api: '/app/find',
    child: [
      { code: 'appCreate', title: '创建应用', api: '/app/create', method: 'post' },
      { code: 'appShow', title: '查看应用', api: '/app/findOne' },
      { code: 'appEnable', title: '暂停/恢复应用', api: '/app/enable', method: 'post' },
      { code: 'appEdit', title: '编辑应用', api: '/app/update', method: 'post' },
      { code: 'appPush', title: '应用回调配置', api: '/app/update', method: 'post' },
      { code: 'appApi', title: '应用服务配置', api: '/app/update', method: 'post' }
    ]
  }, {
    code: 'staff',
    title: '职员',
    api: '/staff/find'
  }]
}, {
  code: 'power',
  title: '权限',
  child: [{
    code: 'admin',
    title: '管理员',
    api: '/admin/find',
    child: [
      { code: 'adminShow', title: '查看管理员', api: '/admin/findOne' },
      { code: 'adminCreate', title: '新增管理员', api: '/admin/create', method: 'post' },
      { code: 'adminEdit', title: '编辑管理员', api: '/admin/update', method: 'post' },
      { code: 'adminPower', title: '管理员角色分配', api: '/admin/update', method: 'post' },
      { code: 'adminRemove', title: '删除管理员', api: '/admin/remove', method: 'post' }
    ]
  }, {
    code: 'role',
    title: '角色',
    api: '/role/find',
    child: [
      { code: 'roleCreate', title: '创建角色', api: '/role/create', method: 'post' },
      { code: 'roleShow', title: '查看角色', api: '/role/findOne' },
      { code: 'roleEdit', title: '编辑角色', api: '/role/update', method: 'post' },
      { code: 'roleRemove', title: '删除角色', api: '/role/remove', method: 'post' }
    ]
  }]
}, {
  code: 'log',
  title: '日志',
  child: [
    { code: 'logLogin', title: '登录日志', api: '/log/find' },
    { code: 'logAdmin', title: '后台日志', api: '/log/find' },
    { code: 'logPush', title: '接受日志', api: '/log/find' },
    { code: 'logApi', title: '接口日志', api: '/log/find' }
  ]
}]

const treeArr = (arr) => {
  let data = {}
  arr.forEach((one) => {
    if (one.api) {
      if (data[one.api]) {
        if (typeof data[one.api] === 'string') data[one.api] = [data[one.api]]
        data[one.api].push(one.code)
      } else {
        data[one.api] = one.code
      }
    }
    if (one.child) Object.assign(data, treeArr(one.child))
  })
  return data
}

const powerApi = treeArr(powerTree)
const apiToCodeFn = {
  '/app/update': (req) => {
    var keys = Object.keys(req.body)
    if (keys.length === 1 && keys[0] === 'apis') return 'appApi'
    if (keys.length === 1 && keys[0] === 'hooks') return 'appPush'
    return 'appEdit'
  },
  '/admin/update': (req) => {
    var keys = Object.keys(req.body)
    if (keys.length === 1 && keys[0] === 'role') return 'adminPower'
    return 'adminEdit'
  },
  '/log/find': (req) => {
    var get = req.query
    if (get.type === 'push') return 'logPush'
    if (get.type === 'login') return 'logLogin'
    if (get.type === 'admin') return 'logAdmin'
    if (get.type === 'api') return 'logApi'
    return 'logFind'
  }
}
Object.assign(powerApi, apiToCodeFn)

// 判断权限
module.exports = {
  apiToCode: powerApi,
  tree: powerTree,
  hasPower: (req, res, next) => {
    // 管理员无需过滤权限
    if (req.user.username === 'admin') return next()
    // Adebug.log(powers, req.baseUrl)
    let code = powerApi[req.baseUrl]
    // 无需权限判断
    if (!code || ['/role/find'].indexOf(req.baseUrl) !== -1) {
      next()
      Adebug.log('公共路由: ', req.baseUrl)
      return
    }
    const powers = ['index']
    if (req.user && req.user.role) powers.push.apply(powers, req.user.role.powers)
    if (typeof code === 'function') code = code(req)
    powers.unshift('')
    const powerStr = powers.join(',')
    if (powerStr.indexOf(',' + code) === -1) {
      Adebug.log('no power: ', code, req.baseUrl, req.query)
      return res.apiErr(30003, { param: code, message: '无权访问' })
    }
    next()
  }
}
